package com.bhs.sansong.ui.order;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.adapter.QuotationAdapter;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.CompaniesInfo;
import com.bhs.sansong.bean.CompaniesInfoData;
import com.bhs.sansong.bean.EnquiryOrderRefresh;
import com.bhs.sansong.bean.OrderCreationSuccess;
import com.bhs.sansong.bean.OrderGoodsDataBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.ui.dialog.BottomSelectPhone2Dialog;
import com.bhs.sansong.ui.wallet.PayDeskActivity;
import com.bhs.sansong.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 报价表
 */
public class QuotationListActivity extends BaseActivity implements NetCallBack {


    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private QuotationAdapter mAdapter;
    private int page=1;
    private String deliver_sn;
    private double lastMoney;
    @Override
    public int initLayout() {
        return R.layout.activity_quotation_list;
    }

    @Override
    public void initView() {
        deliver_sn=getIntent().getStringExtra("deliver_sn");
       TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("查看报价");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
        mAdapter=new QuotationAdapter();
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    public void initData() {
        loadData();
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));

        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);

        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CompaniesInfo data = mAdapter.getData().get(position);
                lastMoney=Double.parseDouble(data.getQuoted_price());
                if(view.getId()==R.id.ll_contact){
                    call(data);
                }else if(view.getId()==R.id.tv_consent){
                    consent(data);
                }
            }
        });
    }

    private void consent(CompaniesInfo data) {
        networkRequest(netApi.accept_quoted(deliver_sn,data.getQuoted_id()),this,9527);
    }

    private BottomSelectPhone2Dialog bottomSelectDialog;
    private BasePopupView basePopupView;
    private void call(CompaniesInfo data) {
        if (basePopupView == null) {
            bottomSelectDialog = new BottomSelectPhone2Dialog(this, data.getMobile(), data.getFixed_telephone(), "");
            basePopupView = new XPopup.Builder(this)
                    .asCustom(bottomSelectDialog)
                    .show();
        } else {
            basePopupView.show();
        }
    }


    Map<String,String> map=new ArrayMap<>();
    private void loadData() {
        map.put("deliver_sn",getIntent().getStringExtra("deliver_sn"));
        map.put("page",String.valueOf(page));
        map.put("limit","10");
        networkRequest(netApi.get_order_quoted_detail(map),this,111);
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if(requestCode==9527){
            OrderCreationSuccess priceBean = new Gson().fromJson(json, OrderCreationSuccess.class);
            if (priceBean.isStatus()) {
                startActivity(new Intent(mActivity, PayDeskActivity.class)
                        .putExtra("pay_sn", priceBean.getData().getPay_sn())
                        .putExtra("deliver_sn", priceBean.getData().getDeliver_sn())
                        .putExtra("money", lastMoney));
                finish();
            } else {
                ToastUtil.show(priceBean.getMsg());
            }
        }else {
            CompaniesInfoData bean = new Gson().fromJson(json, CompaniesInfoData.class);
            mRefreshLayout.setRefreshing(false);
            if (bean.isStatus()) {
                if (page == 1) {
                    mAdapter.setNewData(bean.getData().getList());
                } else {
                    mAdapter.loadMoreComplete();
                    mAdapter.addData(bean.getData().getList());
                }
                if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                    mAdapter.loadMoreEnd();
                }
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }





}